{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "565967d2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-06-27T06:10:59.137001Z",
     "start_time": "2023-06-27T06:10:59.120656Z"
    }
   },
   "outputs": [],
   "source": [
    "# import numpy # 直接导入\n",
    "# from numpy import random  # 导入特定模块\n",
    "import numpy as np  # 写一个小写的名字 \n",
    "# from numpy import * # 导入所有函数  不推荐"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41e7495c",
   "metadata": {},
   "source": [
    "# 数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3cb451bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0c2875ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 5.])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,2,3,4,5.0]) # 只能放一种格式的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "808e9d2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 3, 0, 1])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,0,3,False,True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7972e58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [1], [1, 2, 3, 4, 5, 6, 7]]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[1,2,3],[1],[1,2,3,4,5,6,7]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "deb09bd4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一维数组\n",
    "arr1=np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fd7e942f",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.ndim  # 查看维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9c39ee09",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.shape  # 查看形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "435f26e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [1, 2, 6]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2=np.array([[1,2,3],\n",
    "               [1,2,6]])\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "51cbca02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "01794aae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3e623000",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.size  # 数组里面有多少个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e55c3b68",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 2, 3)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3=np.array([[[1,2,3],[1,2,6]],[[1,2,3],[1,2,6]]])\n",
    "arr3.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78aaed09",
   "metadata": {},
   "source": [
    "# 数组的构建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b1d4866",
   "metadata": {},
   "source": [
    "## 规律数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "329cb130",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(2,8,1)  # start,stop，step"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "a5b28ed2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 3., 4., 5., 6., 7., 8., 9.])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# linspace (start, stop, num=50, endpoint=True,)\n",
    "np.linspace(2,9,8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "b8a3bc33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07, 1.e+08, 1.e+09])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.logspace(2,9,8,base=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "f539ec6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  4.,   8.,  16.,  32.,  64., 128., 256., 512.])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**np.linspace(2,9,8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450b16f1",
   "metadata": {},
   "source": [
    "##  给维度生成数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa2c8a46",
   "metadata": {},
   "source": [
    "### 全是一个数的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "732712ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0.],\n",
       "       [0., 0., 0., 0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones(2) #一维数组\n",
    "np.zeros((6,8)) #二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "938ecb92",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[ 1.49808386e-311,  1.49719303e-311,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 1.49706871e-311, -7.10280088e+188,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 1.49811072e-311,  3.18009339e-271,  0.00000000e+000,\n",
       "           0.00000000e+000]],\n",
       "\n",
       "        [[ 0.00000000e+000,  0.00000000e+000,  1.49706851e-311,\n",
       "          -5.16975741e+203],\n",
       "         [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000]]],\n",
       "\n",
       "\n",
       "       [[[ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000]],\n",
       "\n",
       "        [[ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 1.49808924e-311,  2.64047554e-188,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 0.00000000e+000,  0.00000000e+000,  1.49810764e-311,\n",
       "          -1.69438151e+167]]],\n",
       "\n",
       "\n",
       "       [[[ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 1.49811071e-311,  9.52883453e+235,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000]],\n",
       "\n",
       "        [[ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 1.49811072e-311, -1.72121556e-307,  0.00000000e+000,\n",
       "           0.00000000e+000],\n",
       "         [ 1.49706892e-311,  1.33699089e-266,  0.00000000e+000,\n",
       "           0.00000000e+000]]],\n",
       "\n",
       "\n",
       "       ...,\n",
       "\n",
       "\n",
       "       [[[ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003]],\n",
       "\n",
       "        [[ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003]]],\n",
       "\n",
       "\n",
       "       [[[ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003]],\n",
       "\n",
       "        [[ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003]]],\n",
       "\n",
       "\n",
       "       [[[ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003]],\n",
       "\n",
       "        [[ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003],\n",
       "         [ 7.81250185e-003,  7.81250185e-003,  7.81250185e-003,\n",
       "           7.81250185e-003]]]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty((1000,2,3,4)) #多维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "9ef64948",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[[6, 6, 6, 6],\n",
       "         [6, 6, 6, 6],\n",
       "         [6, 6, 6, 6]],\n",
       "\n",
       "        [[6, 6, 6, 6],\n",
       "         [6, 6, 6, 6],\n",
       "         [6, 6, 6, 6]]]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.full((1,2,3,4),6) # 指定一下填充的数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "66101f15",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-7.0967268e-30,  9.8791542e-43, -7.2086956e-30, ...,\n",
       "        9.8791542e-43, -6.3622560e+35,  9.8791542e-43], dtype=float32)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.empty_like(arr1) # 根据给定数组的形状，生成一个全是XX的数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "bdc96e24",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.eye(6) # 对角矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bb91bcf",
   "metadata": {},
   "source": [
    "### 随机数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "836a1516",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.65610858, 0.56129558, 0.48289465],\n",
       "       [0.89587185, 0.56823034, 0.72993373]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random(2)  # 0~1\n",
    "np.random.random((2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "853ae181",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 7, 2],\n",
       "       [5, 2, 4]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,(2,3)) # 给区间（左闭右开），给形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "dc281193",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5172978838465893"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(23)\n",
    "np.random.random()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "id": "775c8ea0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 4, 2, 5])"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1=np.array([1,2,3,4,5])\n",
    "# np.random.seed(0)\n",
    "np.random.shuffle(arr1)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1e43efb",
   "metadata": {},
   "source": [
    "# numpy 数据读写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44fc861f",
   "metadata": {},
   "source": [
    "## 一个文件一个数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "id": "41a264b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save('存储的数据',arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "id": "1e1722f4",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9647059 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        ...,\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ]],\n",
       "\n",
       "       [[0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.96862745, 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        ...,\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ]],\n",
       "\n",
       "       [[0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.85490197, 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        ...,\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ],\n",
       "        [0.9098039 , 0.9098039 , 0.9098039 , 1.        ]],\n",
       "\n",
       "       ...,\n",
       "\n",
       "       [[0.85490197, 0.85490197, 0.85490197, 1.        ],\n",
       "        [0.85490197, 0.9647059 , 0.85490197, 1.        ],\n",
       "        [0.85882354, 0.85882354, 0.85882354, 1.        ],\n",
       "        ...,\n",
       "        [0.85490197, 0.85490197, 0.85490197, 1.        ],\n",
       "        [0.84705883, 0.84705883, 0.84705883, 1.        ],\n",
       "        [0.84705883, 0.84705883, 0.84705883, 1.        ]],\n",
       "\n",
       "       [[0.85882354, 0.85882354, 0.85882354, 1.        ],\n",
       "        [0.84705883, 0.96862745, 0.84705883, 1.        ],\n",
       "        [0.8509804 , 0.8509804 , 0.8509804 , 1.        ],\n",
       "        ...,\n",
       "        [0.8509804 , 0.8509804 , 0.8509804 , 1.        ],\n",
       "        [0.8509804 , 0.8509804 , 0.8509804 , 1.        ],\n",
       "        [0.85490197, 0.85490197, 0.85490197, 1.        ]],\n",
       "\n",
       "       [[0.85490197, 0.85490197, 0.85490197, 1.        ],\n",
       "        [0.85882354, 0.85490197, 0.85882354, 1.        ],\n",
       "        [0.85490197, 0.85490197, 0.85490197, 1.        ],\n",
       "        ...,\n",
       "        [0.85882354, 0.85882354, 0.85882354, 1.        ],\n",
       "        [0.85490197, 0.85490197, 0.85490197, 1.        ],\n",
       "        [0.8509804 , 0.8509804 , 0.8509804 , 1.        ]]], dtype=float32)"
      ]
     },
     "execution_count": 243,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('存储的数据.npy')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b31d7c96",
   "metadata": {},
   "source": [
    "## 多个数组存到一个文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "id": "161d71fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez('存储的数据.npz',arr1=arr1,数据2=arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "id": "b09a27cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "data=np.load('存储的数据.npz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "id": "9490845a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['arr1', '数据2']"
      ]
     },
     "execution_count": 247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "id": "b6cb7276",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 4, 2, 5])"
      ]
     },
     "execution_count": 249,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['arr1']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b81373d1",
   "metadata": {},
   "source": [
    "# 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "id": "50123cb0",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 5, 9, 0, 2, 7, 2],\n",
       "       [9, 2, 3, 3, 2, 3, 4],\n",
       "       [1, 2, 9, 1, 4, 6, 8],\n",
       "       [2, 3, 0, 0, 6, 0, 6],\n",
       "       [3, 3, 8, 8, 8, 2, 3],\n",
       "       [2, 0, 8, 8, 3, 8, 2],\n",
       "       [8, 4, 3, 0, 4, 3, 6]])"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3=np.random.randint(0,10,(7,7))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "id": "0456d3b2",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 9, 1, 2, 3, 2, 8])"
      ]
     },
     "execution_count": 254,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[0,:] # 一行/一列\n",
    "arr3[:,0] # 一行/一列 \n",
    "# ： 整行、整列，取所有"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "id": "13f210e9",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 2, 3, 3, 2, 3, 4],\n",
       "       [2, 3, 0, 0, 6, 0, 6]])"
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[1:4:2,:]  # 多行/多列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "id": "a109afe8",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 0],\n",
       "       [2, 3],\n",
       "       [2, 1],\n",
       "       [3, 0],\n",
       "       [3, 8],\n",
       "       [0, 8],\n",
       "       [4, 0]])"
      ]
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[:,1:4:2] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "id": "a282abe2",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 5, 2],\n",
       "       [9, 2, 4],\n",
       "       [1, 2, 8],\n",
       "       [2, 3, 6],\n",
       "       [3, 3, 3],\n",
       "       [2, 0, 2],\n",
       "       [8, 4, 6]])"
      ]
     },
     "execution_count": 261,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[:,[0,1,6]]  # 前面不一样 利用列表传递索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "id": "14b66d36",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 8],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[2:6:2,[0,1,6]] # 行列索引交叉 提取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "id": "cf0d90fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[2,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75ba3eb7",
   "metadata": {},
   "source": [
    "# 布尔切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "id": "b28da3cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "bool_arr=arr3<6  # 获得一个形状相同的布尔数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "id": "0a49ecb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 0, 2, 2, 2, 3, 3, 2, 3, 4, 1, 2, 1, 4, 2, 3, 0, 0, 0, 3, 3, 2,\n",
       "       3, 2, 0, 3, 2, 4, 3, 0, 4, 3])"
      ]
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[bool_arr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "id": "202e7119",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 276,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bool_arr.sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "894006ea",
   "metadata": {},
   "source": [
    "# 数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 284,
   "id": "b1ed893e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\wang\\AppData\\Local\\Temp\\ipykernel_22672\\3905340834.py:1: RuntimeWarning: divide by zero encountered in floor_divide\n",
      "  arr3//arr3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 0, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 0, 0, 1, 0, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 0, 1, 1, 1, 1, 1],\n",
       "       [1, 1, 1, 0, 1, 1, 1]])"
      ]
     },
     "execution_count": 284,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3//arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "id": "108353fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 1, 4, 2, 5])"
      ]
     },
     "execution_count": 288,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "id": "d108e2a7",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.73205081, 1.        , 2.        , 1.41421356, 2.23606798])"
      ]
     },
     "execution_count": 287,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sin(arr1)\n",
    "np.sqrt(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "id": "e90c2dac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  2,  8,  4, 10])"
      ]
     },
     "execution_count": 291,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1+arr1\n",
    "np.add(arr1,arr1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f465bbf4",
   "metadata": {},
   "source": [
    "# 广播规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11bc6e08",
   "metadata": {},
   "source": [
    "## 如果两个数组的维度数不相同，那么小维度数组的形状将会在最左边补1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "id": "4aa3adf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 6])"
      ]
     },
     "execution_count": 308,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4=np.array([1,2,3,4])\n",
    "arr4+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "id": "df237ff2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7,  7, 10, 12],\n",
       "       [ 9, 11,  5, 12],\n",
       "       [ 7,  8, 12,  5],\n",
       "       [ 7, 10, 11,  7]])"
      ]
     },
     "execution_count": 309,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5=np.random.randint(0,10,(4,4))\n",
    "arr5+arr4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bb93c96",
   "metadata": {},
   "source": [
    "## 如果两个数组的形状在任何一个维度上都不匹配，那么数组的形状会沿着维度为1的维度扩展以匹配另一个数组的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "id": "13e890bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 5, 3, 4]])"
      ]
     },
     "execution_count": 312,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr6=np.random.randint(0,10,(1,4))\n",
    "arr6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "id": "0fcd44ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5],\n",
       "       [3],\n",
       "       [3],\n",
       "       [7]])"
      ]
     },
     "execution_count": 313,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr7=np.random.randint(0,10,(4,1))\n",
    "arr7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "id": "08f4207d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[12, 10,  8,  9],\n",
       "       [10,  8,  6,  7],\n",
       "       [10,  8,  6,  7],\n",
       "       [14, 12, 10, 11]])"
      ]
     },
     "execution_count": 314,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr6+arr7"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "041e07db",
   "metadata": {},
   "source": [
    "## 如果两个数组的形状在仍何一个维度上都不匹配并且没有仍何一个维度等于1，那么会引发异常"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 316,
   "id": "dda64c01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [2, 3],\n",
       "       [3, 4],\n",
       "       [4, 5]])"
      ]
     },
     "execution_count": 316,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr8=np.array([[1,2],[2,3],[3,4],[4,5]])\n",
    "arr8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "id": "c6107e15",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr9=np.random.randint(0,10,(2,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "id": "23ee39e3",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (2,4) (4,2) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[318], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43marr9\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43marr8\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (2,4) (4,2) "
     ]
    }
   ],
   "source": [
    "arr9+arr8"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
